$(document).ready(function() {
  $('.content hgroup').scrollTop($('.content hgroup').height());
  $('input[type=submit]').click(function() {
    var news = $('input[name=new]').val();
    if (news == "") {
      return false;
    } else if (testing(news)) {
      return false;
    } else {
      var uid = $('input[name=uid]').val();
      var name = $('input[name=name]').val();
      jQuery.ajax({
        url: "plugin.php?id=annual:verify",
        data: {
          action: "talk",
          news: news,
          uid: uid,
          name: name,
        },
        dataType: "json",
        type: "POST",
        async: false,
        success: function(data) {
          //console.log(data);
          if (data == "1") {
            $('input[name=new]').val("").focus();
			   window.location.reload(); 
          }
        }
      });
    }
  });

  //获取留言列表

  function talkList() {
    var nid = $("hgroup section:first-child").attr('value');
    $.ajax({
      type: "POST",
      url: "plugin.php?id=annual:verify",
      data: {
        action: 'newlist',
        nid: nid,
      },
      dataType: "json",
      async: false,
      success: function(data) {
        if (data.news != "") {
          $(".count e").html(data.num);
          newAdd(data);
        }
      }
    });
  }

  /*检测敏感词*/

  function testing(word) {
    var flag = false;
    $.ajax({
      type: "POST",
      url: "plugin.php?id=annual:verify",
      data: {
        action: 'word',
        word: word,
      },
      dataType: "json",
      async: false,
      success: function(data) {
        if (data == 1) {
          flag = true;
        }
      }
    });
    return flag;
  }


  /*留言客户端*/

  function newAdd(data) {
    console.log(data.news.length);
    var newall = data.news;
    for (var i = 0; i < newall.length; i++) {
      var str = "<section value=" + newall[i]['id'] + ">";
      str += "<figure class='pic'><img src='uc_server/avatar.php?uid=" + newall[i]['uid'] + "&size=small'></figure>";
      str += "<aside><h3>" + newall[i]['name'] + "</h3>";
      str += "<p>" + newall[i]['new'] + "</p></aside>";
      str += "</section>";
      $('hgroup').prepend(str);
    }
    $('.content').scrollTop($('.content').height());
  }
  //定时刷新页面数据
  //setInterval(talkList, 3000);
});